/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg_customerManagement;

import pkg_group5.Database;
import java.sql.*;
import java.util.ArrayList;
/**
 *
 * @author mattcarr
 */
public class AddressDatabaseFunctions extends Database {

    public AddressDatabaseFunctions() {
        super("eBookSystem");
    }

    public ArrayList<Address> getAddresses(int customer_id) {
        
        String query = "SELECT * from addresses where customer_id = ?";
        
        ArrayList<Address> addresses = new ArrayList();
        
        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setInt(1, customer_id);
            
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Address a = new Address();
                a.setId(rs.getInt(1));
                a.setCustomer_id(rs.getInt(2));
                a.setAddrLineOne(rs.getString(3));
                a.setAddrLineTwo(rs.getString(4));
                a.setCity(rs.getString(5));
                a.setPostCode(rs.getString(6));
                a.setCounty(rs.getString(7));
                a.setCountry(rs.getString(8));
                
                addresses.add(a);
            }
            
            return addresses;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }
    
    public int removeAddress(int address_id)
    {
        String query = "DELETE FROM addresses WHERE address_id = ?";
        
        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setInt(1, address_id);
                       
            int status = ps.executeUpdate();

            if (status == 1) {
                return 0;
            } else {
                return -1;
            }
            
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
    
    public int updateAddress(int address_id, int customer_id, String lineOne, String lineTwo, String city, String post_code, String county, String country) {
        
        String query = "UPDATE addresses SET customer_id = ?, address_line_one = ?, address_line_two =?, city =?, post_code = ?, county = ?, country = ? WHERE address_id = ?";
        
        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setInt(1, customer_id);
            ps.setString(2, lineOne);
            ps.setString(3, lineTwo);
            ps.setString(4, city);
            ps.setString(5, post_code);
            ps.setString(6, county);
            ps.setString(7, country);
            
            ps.setInt(8, address_id);
            
            int status = ps.executeUpdate();

            if (status == 1) {
                return 0;
            } else {
                return -1;
            }
            
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }
    
    public int addAddress(int customer_id, String lineOne, String lineTwo, String city, String post_code, String county, String country) {

        String query = "INSERT INTO addresses (customer_id, address_line_one, address_line_two, city, post_code, county, country) VALUES (?, ?, ?, ?, ?, ?, ?)";

        try {
            PreparedStatement ps = conn.prepareStatement(query);
            ps.clearParameters();
            ps.setInt(1, customer_id);
            ps.setString(2, lineOne);
            ps.setString(3, lineTwo);
            ps.setString(4, city);
            ps.setString(5, post_code);
            ps.setString(6, county);
            ps.setString(7, country);

            int status = ps.executeUpdate();

            if (status == 1) {
                return 0;
            } else {
                return -1;
            }

        } catch (java.sql.SQLException e) {
            System.out.println(e);
            return -1;
        } catch (java.lang.NullPointerException e) {
            return -1;
        }

    }
}
